******************************************************************************************
* This script prepares
* 		- data for figure 5
* 		- figures 4 & 6
******************************************************************************************

clear all
set more off
* net install labutil.pkg
*global rootfolder ""
global inputfolder "$rootfolder\figures\simulations"
global chartfolder "$rootfolder\figures\charts"

******************************************************************************************
** Import & add labels
******************************************************************************************
insheet using "$inputfolder\outputMatlab_simulations.csv", comma 

rename dppi* Dppi* 
label var year "Year"
label var country "Reporting country abr."
label var country_ind "Reporting country industry (NACE code)"

gen country_lower = lower(country)
levelsof(country_lower), local(levels_country)
foreach countr of local levels_country  {
	label var Dppi_shock_comp_`countr' "complete log-change PPI resulting from cost shocks in `countr'"
	label var Dppi_shock_dir_`countr' "direct log-change PPI from cost shocks in `countr' (no network)"
	label var cost_shock_`countr' "Uniform cost shock in `countr' resulting in 1% change in `countr' PPI"
}
drop country_lower

label var cost_shock_energy "Uniform cost shock in the energy sector (NACE 23, s_I=6) resulting in 10% change in energy PPI"
label var Dppi_shock_comp_energy "log-change PPI resulting from cost shocks in energy sector"
label var Dppi_shock_dir_energy "log-change PPI resulting from cost shocks in energy sector (no network)"

format year %ty

*save a stata version of the matlab output
save "$inputfolder\GVC_maindata_with_simulated_shocks.dta", replace

******************************************************************************************
** Prepare data for Figure 5
******************************************************************************************
** in prevision of graph of effect by each country to country, generate average effect by country
** (without ROW)

use "$inputfolder\GVC_maindata_with_simulated_shocks.dta", clear

keep Dppi_shock* country total_output
drop Dppi_shock_comp_energy Dppi_shock_dir_energy
drop Dppi_shock_dir*
rename Dppi_shock_comp* Dppi_shock*

drop if country == "ROW"
drop Dppi_shock_row

collapse (sum) total_output (mean) Dppi_shock* [iweight=total_output], by(country)

gen avg_effect_on_others = 0
gen country_lower = lower(country)
levelsof(country_lower), local(levels_country)

foreach countr of local levels_country  {

	quietly sum Dppi_shock_`countr' if country_lower != "`countr'"
	scalar tempscalar_mean = r(mean)
	replace avg_effect_on_others = tempscalar_mean if country_lower == "`countr'"
}
replace avg_effect_on_others = abs(avg_effect_on_others)
sort avg_effect_on_others

*prepare ordering
*for ordering we need the average effect FROM foreign shocks
egen avg_effect_from_others = rowtotal(Dppi_*)

foreach countr of local levels_country  {
	replace avg_effect_from_others = avg_effect_from_others - Dppi_shock_`countr' if  country_lower == "`countr'"
}
replace avg_effect_from_others = avg_effect_from_others/30

* sort by effect on others
sort avg_effect_on_others
local ordering_list country country_lower
local ordering_list2 country country_lower country_column

*get the ordering we want by taking countries
foreach ii of num 1/30 {
	local temp = country_lower[`ii']
	local ordering_list `ordering_list' Dppi_shock_`temp'
	local ordering_list2 `ordering_list2' Dppi_shock_`temp'
	local country_column_loc`ii' `temp' 
}

order `ordering_list'
*sort by effect FROM others (in descending order)
gen avg_effect_from_others_opp = -avg_effect_from_others
sort avg_effect_from_others_opp

*create a variable country_column
gen country_column = ""
foreach ii of num 1/30 {
	replace country_column = "`country_column_loc`ii''" in `ii'
}
order `ordering_list2'
replace country_column = upper(country_column)

* save as dta if needed
save "$inputfolder\matrix_dppi_for_matlab_graph.dta", replace
* outsheet to matlab readable
outsheet using "$inputfolder\matrix_dppi_for_matlab_graph.csv", comma replace


* Reshape to get the relative size of the network effect
use "$inputfolder\GVC_maindata_with_simulated_shocks.dta", clear

keep Dppi_shock* country total_output
drop Dppi_shock_comp_energy Dppi_shock_dir_energy

collapse (sum) total_output (mean) Dppi_shock* [iweight=total_output], by(country)

reshape long Dppi_shock_comp_ Dppi_shock_dir_ , i(country) j(source_country) string

drop total_output
rename country destination_country
rename Dppi_shock_comp_ Dppi_shock_comp 
rename Dppi_shock_dir_ Dppi_shock_dir

label var Dppi_shock_comp "complete log-change PPI resulting from 1% cost shocks in source country"
label var Dppi_shock_dir "direct log-change PPI resulting from 1% cost shocks in source country (no network)"

replace source = strupper(source)

save "$inputfolder\matrix_dppi_for_matlab_3dgraph.dta", replace

******************************************************************************************
* Figures 4&6
******************************************************************************************
use "$inputfolder\GVC_maindata_with_simulated_shocks.dta", clear

keep Dppi_shock* country total_output
collapse (sum) total_output (mean) Dppi_shock* [iweight=total_output], by(country)

******************************************************************************************
*Figure 4: Spillovers from cost shocks to selected countires and energy prices
******************************************************************************************
*4a-c: shock in the USA, JPN, CHN
foreach letter in a b c d {
	if "`letter'" == "a" {
		local shockvarname usa
		local shockgraphname USA
	}
	if "`letter'" == "b" {
		local shockvarname jpn
		local shockgraphname JPN
	}
	if "`letter'" == "c" {
		local shockvarname chn
		local shockgraphname CHN
	}
	if "`letter'" == "d" {
		local shockvarname energy
		local shockgraphname energy
	}
	if "`letter'" != "d" {
		gen plotDppi_shock_comp_`shockvarname' = Dppi_shock_comp_`shockvarname'
		gen plotDppi_shock_dir_`shockvarname' = Dppi_shock_dir_`shockvarname'
	}
	else {
		gen plotDppi_shock_comp_`shockvarname' = Dppi_shock_comp_`shockvarname'
		gen plotDppi_shock_dir_`shockvarname' = Dppi_shock_dir_`shockvarname'
	}
	gen plotDppi_shock_ind_`shockvarname' = plotDppi_shock_comp_`shockvarname' - plotDppi_shock_dir_`shockvarname'

	capture drop tmp_rank rank
	if "`letter'" != "d" {
		egen tmp_rank = rank(-Dppi_shock_comp_`shockvarname') 
	}
	else {
		egen tmp_rank = rank(-Dppi_shock_comp_`shockvarname') 
	}
	
	gen rank = tmp_rank
	sort rank
	labmask rank, values(country)
	
	graph hbar plotDppi_shock_comp_`shockvarname' if country !="ROW" & country !="`shockgraphname'", over(rank,lab(angle(horizontal) labsize(vsmall))) ytitle("%points {&Delta}PPI") ///
		legend(off)  ///
		title("") graphregion(fcolor(white) lcolor(white))
	graph export "$chartfolder\fig4_`shockgraphname'_shock_full_ex1.eps", as(eps) replace
	graph export "$chartfolder\fig4_`shockgraphname'_shock_full_ex1.pdf", as(pdf) replace
	
	drop plotDppi_shock_comp_`shockvarname' plotDppi_shock_dir_`shockvarname' plotDppi_shock_ind_`shockvarname'
}

******************************************************************************************
* Figure 6: spillovers from a 1% inflationary shock in every other country
*** (13.01.2017) +  direct and indirect effect 
******************************************************************************************
use "$inputfolder\GVC_maindata_with_simulated_shocks.dta", clear
keep Dppi_shock* country total_output
drop Dppi_shock_comp_energy Dppi_shock_dir_energy

collapse (sum) total_output (mean) Dppi_shock* [iweight=total_output], by(country)

gen country_lower = lower(country)
levelsof(country_lower), local(levels_country)
foreach cc of local levels_country {
	replace Dppi_shock_comp_`cc' = Dppi_shock_comp_`cc' 
	replace Dppi_shock_dir_`cc' = Dppi_shock_dir_`cc' 
}

keep country Dppi_shock_comp_* Dppi_shock_dir_*
reshape long Dppi_shock_comp_ Dppi_shock_dir_, i(country) j(source0) string
gen source = upper(source0)
drop source0
drop if source == "ROW"
drop if country == "ROW"

egen glob_comp = sum(Dppi_shock_comp_) if country~=source, by(country)
egen glob_dir = sum(Dppi_shock_dir_) if country~=source, by(country)

levelsof country, local(cnt)

gen glob_comp_avg = .
gen glob_dir_avg = .

foreach c of local cnt {

	egen glob_comp_`c'_0 = sum(Dppi_shock_comp_) if country~="`c'" & source~="`c'", by(country)
	egen glob_comp_`c' = mean(glob_comp_`c'_0)
	replace glob_comp_avg = glob_comp_`c' if country == "`c'"
	drop glob_comp_`c'*
	
	egen glob_dir_`c'_0 = sum(Dppi_shock_dir_) if country~="`c'" & source~="`c'", by(country)
	egen glob_dir_`c' = mean(glob_dir_`c'_0)
	replace glob_dir_avg = glob_dir_`c' if country == "`c'"
	drop glob_dir_`c'*
	
}

gen glob_comp_renormalized = glob_comp/glob_comp_avg
gen glob_dir_renormalized = glob_dir/glob_dir_avg

* Reshape wide back
drop Dppi* source
collapse (mean) glob*, by(country)
gen glob_ind_renormalized = glob_comp_renormalized - glob_dir_renormalized

capture drop tmp_rank rank
egen tmp_rank = rank(-glob_comp_renormalized) 
gen rank = tmp_rank
sort rank
labmask rank, values(country)

graph hbar glob_comp_renormalized glob_dir_renormalized glob_ind_renormalized, over(rank,lab(angle(horizontal) labsize(vsmall))) ytitle("%points {&Delta}PPI") ///
	bar(2, fcolor(white) lcolor(black)) bar(3, fcolor(gs7) lcolor(gs7)) ///
	title("") graphregion(fcolor(white) lcolor(white)) ///
	legend(order( 1 "Total effect" 2 "Direct effect" 3 "Indirect effect") size(small))

graph export "$chartfolder\fig6_NEW_shock_allothersNoROW_full_DIT.eps", as(eps) replace
graph export "$chartfolder\fig6_NEW_shock_allothersNoROW_full_DIT.pdf", as(pdf) replace


